package _11_位运算;

public class _191_位1的个数 {

    public static void main(String[] args) {

        _191_位1的个数 v = new _191_位1的个数();

        System.out.println(v.hammingWeight(-3));
    }

    // 加速，计算
    public int hammingWeight(int n) {
        int count = 0;
        while (n != 0) {
            n &= n - 1;
            count++;
        }
        return count;
    }

    // 遍历获取0的个数
    public int hammingWeight1(int n) {
        int count = 0;
        while (n != 0) {
            if ((n & 1) != 0) {
                count++;
            }
            n >>>= 1;
        }
        return count;
    }

}
